package common;

import java.util.Collection;
import java.util.HashMap;
import model.AltaRicaConst;
import model.AltaRicaDomain;
import model.AltaRicaNode;
import parser.AltaRicaParseModel;
import parser.AltaRicaParseNode.CycleDetectionException;

public class AltaRicaModel {
	
	private HashMap<String, AltaRicaNode> nodes = new HashMap<String,AltaRicaNode>();
	private HashMap<String, AltaRicaDomain> domains = new HashMap<String,AltaRicaDomain>();
	private HashMap<String, AltaRicaConst> consts = new HashMap<String,AltaRicaConst>();
	private AltaRicaParseModel _ast = null;
        
        private AltaRicaModel(){
            
        }
        public static AltaRicaModel compileFromAST(AltaRicaParseModel parseModel, boolean addToModel) throws CycleDetectionException {
            AltaRicaModel newInstance = null;
            if (parseModel != null) {
                newInstance = new AltaRicaModel();
                if (addToModel) {
                	AltaRicaModelManager.getInstance().addModel(newInstance);
                }
                newInstance._ast = parseModel;
                newInstance._ast.compile(newInstance);
                
            }
            return newInstance;
        }
        
        public AltaRicaParseModel getAst() {
            return this._ast;
        }
        
	public void addNode(AltaRicaNode node) {
		
		nodes.put(node.getName(), node);
	}
	public void removeNode(AltaRicaNode node) {
		nodes.remove(node.getName());
	}
	
	public AltaRicaNode getNode(String id) {
		
		return nodes.get(id);
	}
	
	public Collection<AltaRicaNode> getNodes() {
		
		return nodes.values();
	}
	public void clear()
        {
            domains.clear();
            nodes.clear();
            consts.clear();
        }
	public void addDomain(AltaRicaDomain domain) {
		
		domains.put(domain.getName(), domain);
	}
	
	public AltaRicaDomain getDomain(String name) {
		
		return domains.get(name);
	}
	
	public Collection<AltaRicaDomain> getDomains() {
		
		return domains.values();
	}
	
	public void addConst(AltaRicaConst cnst) {
		
		consts.put(cnst.getId(), cnst);
	}
	
	public AltaRicaConst getConst(String id) {
		
		return consts.get(id);
	}
	
	public Collection<AltaRicaConst> getConstants() {
		
		return consts.values();
	}
	
	public String string() {
		
		String str = new String();
		
		for (AltaRicaConst cnst : consts.values()) {
			
			str += cnst.toString() + "\n";
		}
		
		str += "\n"; 
		
		for (AltaRicaDomain domain : domains.values()) {
			
			str += domain.toString() + "\n";
		}
		
		str += "\n";
		
		for (AltaRicaNode node : nodes.values()) {
			str += node.string();
		}
		
		return str;
	}

      

}
